<template>
  <div class="bilibili-container">
    <!-- 原始导航栏（正常文档流） -->
    <div class="original-nav" ref="originalNav" :style="{ visibility: isSticky ? 'hidden' : 'visible' }">
      <div class="nav-content">
        <div class="left-section">

          <span v-for="item in leftNavItems" :key="item.id" class="nav-item"
            :class="{ active: $route.path === item.path }" @click="navigateTo(item.path)">
            <i class="iconfont" :class="item.icon"></i>
            {{ item.name }}
          </span>
        </div>

        <div class="middle-section">
          <div class="search-box">
            <el-input v-model="searchText" placeholder="每天都有新发现" class="search-input">
              <template #append>
                <el-button :icon="Search" />
              </template>
            </el-input>
          </div>
        </div>

        <div class="right-section">
          <div class="user-section">
            <span v-if="!authStore.isLoggedIn" class="nav-item login-bg">
              <span @click="openLoginDialog">登录</span>
            </span>
            <el-dropdown v-else>
              <span class="nav-item">
                <el-avatar :size="36" :src="authStore.userInfo.avatar" />
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>个人中心</el-dropdown-item>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
          <span class="nav-item">
            <i class="iconfont icon-dahuiyuan"></i>
            <span>大会员</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-xiaoxi"></i>
            <span @click="goPushMes">消息</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-dongtai"></i>
            <span @click="goPushDyc">动态</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-shoucang1"></i>
            <span>收藏</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-lishi"></i>
            <span @click="goPushHistroy">历史</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-chuangzuozhongxin"></i>
            <span>创作中心</span>
          </span>
        </div>
      </div>
    </div>

    <!-- 吸顶导航栏（固定定位） -->
    <div class="sticky-nav" :class="{ 'sticky-active': isSticky }">
      <div class="nav-content">
        <!-- 与原始导航相同的内容 -->
        <div class="left-section">
          <span v-for="item in leftNavItems" :key="item.id" class="nav-item" :class="{ active: item.id === 1 }">
            {{ item.name }}
          </span>
        </div>

        <div class="middle-section">
          <div class="search-box">
            <el-input v-model="searchText" placeholder="每天都有新发现" class="search-input">
              <template #append>
                <el-button :icon="Search" />
              </template>
            </el-input>
          </div>
        </div>

        <div class="right-section">
          <div class="user-section">
            <span v-if="!authStore.isLoggedIn" class="nav-item login-bg">
              <span @click="openLoginDialog">登录</span>
            </span>
            <el-dropdown v-else>
              <span class="nav-item">
                <el-avatar :size="36" :src="authStore.userInfo.avatar" />
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>个人中心</el-dropdown-item>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
          <span class="nav-item">
            <i class="iconfont icon-dahuiyuan"></i>
            <span>大会员</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-xiaoxi"></i>
            <span>消息</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-dongtai"></i>
            <span>动态</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-shoucang1"></i>
            <span>收藏</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-lishi"></i>
            <span>历史</span>
          </span>
          <span class="nav-item">
            <i class="iconfont icon-chuangzuozhongxin"></i>
            <span>创作中心</span>
          </span>
        </div>
      </div>
    </div>

    <!-- 带背景图的banner -->
    <!-- <div class="bilibili-banner">
      背景图层
      <div class="layer" v-for="(item, index) in layers" :key="index" :style="getLayerStyle(item)">
        <img v-if="item.type === 'image'" :src="item.src" :alt="'layer-' + index">
      </div>
    </div>-->
  </div>
  <!-- 登录弹窗组件 -->
  <LoginDialog ref="loginDialog" />
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { ref, onMounted, onUnmounted } from 'vue'
import { throttle } from 'lodash-es'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import LoginDialog from '@/components/LoginDialog.vue'

const router = useRouter()
const authStore = useAuthStore()
const searchText = ref('')
const originalNav = ref(null)
const isSticky = ref(false)
const loginDialog = ref(null)
const visible = ref(false)
const openLoginDialog = () => {
  loginDialog.value?.open()
}

const handleLogout = () => {
  authStore.logout()
  ElMessage.success('已退出登录')
}
// 导航方法
const navigateTo = (path) => {
  router.push(path)
}
//消息
const goPushMes = () => {
  router.push('./Message')

}
//动态
const goPushDyc = () => {
  router.push('./Dynamic')
}
//历史
const goPushHistroy = () => {
  router.push('./Histroy')
}
// 导航数据
// 导航数据 with paths
const leftNavItems = [
  { id: 1, name: '赛事', path: '' },
  { id: 2, name: '主站', path: '/HomeView' },
  { id: 3, name: '赛事中心', path: '' },
  { id: 4, name: '英雄联盟', path: '' },
  { id: 5, name: '王者荣耀', path: '' },
  { id: 6, name: '第五人格', path: '' },
  { id: 7, name: '无畏契约', path: '' },
  { id: 8, name: 'CSGO', path: '' },
  { id: 9, name: '守望先锋', path: '' },
  { id: 10, name: '更多赛事', path: '' }
]

// 背景图层数据
// const layers = ref([
//   {
//     type: 'image',
//     src: 'https://i0.hdslb.com/bfs/vc/081110f6c4c54ab35e31cf0eb7bd0b129ee4135c.png@1c.webp',
//     height: 180,
//     width: 1920,
//     transform: 'translate(0, 0)',
//     opacity: 1,
//     zIndex: 1
//   },
//   {
//     type: 'image',
//     src: 'https://i0.hdslb.com/bfs/vc/72d21b0354fd65c2cd7887aeab0eab826b690015.png@1c.webp',
//     height: 180,
//     width: 1920,
//     transform: 'translate(0, 0)',
//     opacity: 1,
//     zIndex: 2
//   }
// ]);

// const getLayerStyle = (item) => {
//   return {
//     transform: item.transform,
//     opacity: item.opacity,
//     height: `${item.height}px`,
//     width: `${item.width}px`,
//     zIndex: item.zIndex
//   }
// }

// 滚动处理
const handleScroll = throttle(() => {
  if (!originalNav.value) return

  const navRect = originalNav.value.getBoundingClientRect()
  isSticky.value = navRect.bottom <= 0
}, 100)

onMounted(() => {
  window.addEventListener('scroll', handleScroll)
})

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll)
})
defineExpose({
  open: () => {
    visible.value = true
  }
})
</script>

<style lang="scss" scoped>
.bilibili-container {
  // height: 200px;
  position: relative;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  // background-image: url('@/assets/imgs/home_view/bg_img.png');
  // object-fit: cover;


  /* 原始导航栏样式 */
  .original-nav {
    width: 100%;
    height: 50px;
    // background-color: rgba(255, 255, 255, 0.9);
    // opacity: .3;

    position: relative;
    z-index: 100;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

    .nav-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 100%;
      padding: 0 24px;
      max-width: 1400px;
      margin: 0 auto;

    }
  }

  /* 吸顶导航栏样式 */
  .sticky-nav {
    position: fixed;
    top: -60px;
    left: 0;
    width: 100%;
    height: 50px;
    // background-color: white;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    transition: top 0.3s ease;

    .nav-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 100%;
      padding: 0 24px;
      max-width: 1400px;
      margin: 0 auto;
    }

    &.sticky-active {
      top: 0;
    }
  }

  /* 左侧导航 */
  .left-section {
    display: flex;
    align-items: center;
    gap: 20px;
    color: white;

    .nav-item {
      font-size: 14px;
      color: #222;
      cursor: pointer;
      transition: color 0.2s;
      white-space: nowrap;
      padding: 0 8px;
      height: 50px;
      line-height: 50px;

      &:hover {
        color: #00a1d6;
      }

      &.active {
        color: #00a1d6;
        font-weight: 500;
        border-bottom: 2px solid #00a1d6;
      }
    }
  }

  /* 搜索框 */
  .middle-section {
    flex: 1;
    max-width: 500px;
    padding: 0 20px;

    .search-box {
      width: 100%;
      position: relative;

      .search-input {
        width: 100%;
        border-radius: 8px;
        overflow: hidden;

        :deep(.el-input__wrapper) {
          box-shadow: none;
          border: 1px solid #e7e7e7;
          height: 32px;
          padding-left: 12px;

          &:hover {
            border-color: #00a1d6;
          }
        }

        :deep(.el-input-group__append) {
          background-color: #00a1d6;
          color: white;
          border: none;
          padding: 0 12px;
          box-shadow: none;

          .el-button {
            padding: 0;
          }
        }
      }
    }
  }

  /* 右侧导航（带图标） */
  .right-section {
    display: flex;
    align-items: center;
    gap: 15px;
    color: white;

    .nav-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      font-size: 12px;
      color: #222;
      cursor: pointer;
      transition: all 0.2s;
      min-width: 40px;
      height: 50px;

      i {
        margin-bottom: 2px;
        font-size: 18px;
        transition: color 0.2s;
      }

      span {
        line-height: 1;
      }

      &:hover {
        color: #00a1d6;

        i {
          color: #00a1d6;
        }
      }
    }

    //登录圆形框
    .login-bg {
      span {

        width: 36px;
        height: 36px;
        background-color: #00a1d6;
        color: white;
        font-weight: 800;
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }

    .user-section {
      display: flex;
      align-items: center;

      .nav-item {
        flex-direction: row;
        gap: 4px;
        min-width: auto;
        padding: 0 8px;

        i {
          margin-bottom: 0;
          font-size: 16px;
        }

        span {
          font-size: 14px;
        }
      }
    }
  }

  /* 带背景图的banner */
  .bilibili-banner {
    position: relative;
    width: 100%;
    height: 180px;
    overflow: hidden;
    background: linear-gradient(120deg, #00a1d6 0%, #f25d8e 100%);

    .layer {
      position: absolute;
      top: 0;
      left: 0;
      transition: all 0.3s ease;

      // img {
      //   width: 100%;
      //   height: 100%;
      //   object-fit: cover;
      // }
    }
  }
}


/* 动画效果 */
@keyframes float {
  0% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-8px);
  }

  100% {
    transform: translateY(0);
  }
}

.bilibili-banner:hover .layer:nth-child(odd) {
  animation: float 4s ease-in-out infinite;
}
</style>
